﻿// ---------------------------------------------------------------------
// Lớp ETurnManager
//
// Nội dung:
//    + Quản lý các script
//
// Copyright (c) 2009 YugiOH
//
// Nguyễn Hữu Lê Trọng Tín      YM: ilu2009             ĐT: 0906520611 
// Võ Phước Hậu                 YM: phuochauspkt        ĐT: 0979202707
// ---------------------------------------------------------------------

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ManagementEffect.Engine;
using Common;
using ServerYuGi;



namespace ManagementEffect
{
    public class ETurnManager
    {
        public Dictionary<YCheckPoint, Base_CheckPoint> Factory = new Dictionary<YCheckPoint, Base_CheckPoint>();
        bool Is_CheckNoAsk;
             
        public ETurnManager()
        {
            Init();
        }

        public void Init()
        {
            
            Array tmp = Enum.GetValues(typeof(YCheckPoint));
            foreach (object item in tmp)
            {
                Factory[(YCheckPoint)item] = new Base_CheckPoint();
            }

        }

        

        /// <summary>
        /// Đăng kí 1 effect mới
        /// </summary>
        /// <param name="effect">Loại Effect</param>
        /// <param name="id">ID lá bài</param>
        /// <param name="script">script thực thi</param>
        public void Register(YCheckPoint effect, int id, string scriptCheck,string scriptExe,TypeCheck type)
        {
            if (Factory[effect] != null)
                Factory[effect].DanhSach.Add( new Base_CheckPoint.Item_Effect{ID= id,ScriptCheck = scriptCheck,ScriptExe= scriptExe,typeCheck = type, isAsk=true});
        }

        public void RegisterNoAsk(YCheckPoint effect, int id, string scriptCheck, string scriptExe, TypeCheck type)
        {
            if (Factory[effect] != null)
                Factory[effect].DanhSachNoAsk.Add(new Base_CheckPoint.Item_Effect { ID = id, ScriptCheck = scriptCheck, ScriptExe = scriptExe, typeCheck = type, isAsk=false });
        }
        /// <summary>
        /// Thực thi List Script
        /// </summary>
        /// <param name="effect">Loại Effecy</param>
        /// <param name="engine">Engine Python</param>
        public void Active(YCheckPoint effect,PythonEngine engine)
        {
            if (Factory[effect] != null)
                Factory[effect].Active(engine);
        }       
        
        public void ActiveWithID(YCheckPoint effect,int ID,PythonEngine engine)
        {
            if (Factory[effect] != null)
                Factory[effect].ActiveWithID(ID, engine);
        }
        public void ActiveWithScript(string ScriptExcute, PythonEngine engine)
        {
            Base_CheckPoint.ActiveWithScript(ScriptExcute, engine);
        }

        public void Check(YCheckPoint effect,PythonEngine engine,SSupervisor super)
        {
            Is_CheckNoAsk = false;
            if (Factory[effect] != null)
                Factory[effect].Check(engine,super);
        }
        public void CheckNoAsk(YCheckPoint effect, PythonEngine engine, SSupervisor super)
        {
            Is_CheckNoAsk = true;
            if (Factory[effect] != null)
                Factory[effect].CheckNoAsk(engine, super);
        } 
        public void RemoveScript(int id)
        {
            Console.WriteLine("Xoa Script:{0}", id);
            foreach (Base_CheckPoint value in Factory.Values)
            {
                for (int i = 0; i < value.DanhSach.Count; i++)
                {
                    if (value.DanhSach[i].ID == id)
                    {
                        value.XoaSauActive.Enqueue(value.DanhSach[i]);
                    }
                }
                for (int i = 0; i < value.DanhSachNoAsk.Count; i++)
                {
                    if (value.DanhSachNoAsk[i].ID == id)
                    {
                        value.XoaSauActive.Enqueue(value.DanhSachNoAsk[i]);
                    }
                }
            }
        }
        public string GetScriptExecute(YCheckPoint checkpoint,int ID)
        {
            string result = "";
            if (Factory[checkpoint] != null)
                result = Factory[checkpoint].GetScriptExecute(ID);
            return result;
        }
        public void F5()
        {
            foreach (Base_CheckPoint value in Factory.Values)
            {
                value.DanhSach.Clear();
                value.DanhSachNoAsk.Clear();
                value.XoaSauActive.Clear();
            }
        }
        //  [2/25/2010 TinNguyen]
        //-------------------------------------------------------------------------------------------------//
        public Dictionary<YCheckPoint, Base_CheckPoint> GetFactory()
        {
            return Factory;
        }
        public bool Is_Check_No_Ask
        {
            get { return Is_CheckNoAsk; }
        }
    }
}
